﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class admin_Clients : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (btnSave.CommandArgument == "")
        {
            tbl_client_details model = new tbl_client_details();
            model.USER_CODE = txtUSER_CODE.Text.Trim();
            model.COMPANY_NAME = txtCOMPANY_NAME.Text.Trim();
            model.COMPANY_ADDRESS = txtCOMPANY_ADDRESS.Text.Trim();
            model.Country = ddlCountry.SelectedValue;
            model.STATE = ddlSTATE.SelectedValue;
            model.CITY = ddlCITY.SelectedValue;
            model.POSTAL_CODE = txtPOSTAL_CODE.Text.Trim();
            model.PRIMARY_TELEPHONE = txtPRIMARY_TELEPHONE.Text.Trim();
            model.SECONDARY_TELEPHONE = txtSECONDARY_TELEPHONE.Text.Trim();
            model.PRIMARY_MOBILE = txtPRIMARY_MOBILE.Text.Trim();
            model.SECONDARY_MOBILE = txtSECONDARY_MOBILE.Text.Trim();
            model.Email = txtEmail.Text.Trim();
            ////////////////////////////////////
            if(fuAgreement_Document.HasFile)
                model.Agreement_Document = fuAgreement_Document.PostedFile.FileName;
            else
                model.Agreement_Document = "";

            model.Agreement_Document_GUID = uploadFiles("Agreement");

            if (fuLink_To_ISF.HasFile)
                model.Link_To_ISF = fuLink_To_ISF.PostedFile.FileName;
            else
                model.Link_To_ISF = "";

            model.Link_To_ISF_GUID = uploadFiles("ISF");
            ////////////////////////////////////
            model.FAX = txtFAX.Text.Trim();
            model.LinkedIn_ID = txtLinkedIn_ID.Text.Trim();
            model.Skype_ID = txtSkype_ID.Text.Trim();
            model.VAT_Tax_ID = txtVAT_Tax_ID.Text.Trim();
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;
            model.Active = Convert.ToBoolean(chkActive.Checked);



            BLL_tbl_client_details bll = new BLL_tbl_client_details();
            try
            {



                bll.insert(model);
                string script = "top.location=" + '"' + HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + Request.RawUrl + '"';
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Record Saved Succesfully');", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Postback", script, true);

                return;
            }
            catch (Exception)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
                return;
            }
        }
        else
        {
            tbl_client_details model = new tbl_client_details();
            model.USER_CODE = btnSave.CommandArgument.Trim();
            model.COMPANY_NAME = txtCOMPANY_NAME.Text.Trim();
            model.COMPANY_ADDRESS = txtCOMPANY_ADDRESS.Text.Trim();
            model.Country = ddlCountry.SelectedValue;
            model.STATE = ddlSTATE.SelectedValue;
            model.CITY = ddlCITY.SelectedValue;
            model.POSTAL_CODE = txtPOSTAL_CODE.Text.Trim();
            model.PRIMARY_TELEPHONE = txtPRIMARY_TELEPHONE.Text.Trim();
            model.SECONDARY_TELEPHONE = txtSECONDARY_TELEPHONE.Text.Trim();
            model.PRIMARY_MOBILE = txtPRIMARY_MOBILE.Text.Trim();
            model.SECONDARY_MOBILE = txtSECONDARY_MOBILE.Text.Trim();
            model.Email = txtEmail.Text.Trim();

            ////////////////////////////////////
            if (fuAgreement_Document.HasFile)
            {
                model.Agreement_Document = fuAgreement_Document.PostedFile.FileName;
                model.Agreement_Document_GUID = uploadFiles("Agreement");
            }
            else
            {
                model.Agreement_Document = txtAgreement_Document.Attributes["OriginalFileName"];
                model.Agreement_Document_GUID = txtAgreement_Document.Attributes["GuidFileName"];
            }

            if (fuLink_To_ISF.HasFile)
            {
                model.Link_To_ISF = fuLink_To_ISF.PostedFile.FileName;
                model.Link_To_ISF_GUID = uploadFiles("ISF");
            }
            else
            {
                model.Link_To_ISF = txtLink_To_ISF.Attributes["OriginalFileName"];
                model.Link_To_ISF_GUID = txtLink_To_ISF.Attributes["GuidFileName"];
            }
            ////////////////////////////////////

            model.FAX = txtFAX.Text.Trim();
            model.LinkedIn_ID = txtLinkedIn_ID.Text.Trim();
            model.Skype_ID = txtSkype_ID.Text.Trim();
            model.VAT_Tax_ID = txtVAT_Tax_ID.Text.Trim();
            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;
            model.Active = Convert.ToBoolean(chkActive.Checked);



            BLL_tbl_client_details bll = new BLL_tbl_client_details();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    private string uploadFiles(string docType)
    {
        string guid = "";
        string guidPath = "";
        bool isFileSaved = false;
        if (docType == "Agreement")
        {
            if (fuAgreement_Document.HasFile)
            {
                guid = Guid.NewGuid().ToString() + Path.GetExtension(fuAgreement_Document.FileName);
                guidPath = Server.MapPath("~/AllFiles/Clients/Agreement/" + guid);
                fuAgreement_Document.SaveAs(guidPath);
                isFileSaved = true;
            }
        }
        else if (docType == "ISF")
        {
            if (fuLink_To_ISF.HasFile)
            {
                guid = Guid.NewGuid().ToString() + Path.GetExtension(fuLink_To_ISF.FileName);
                guidPath = Server.MapPath("~/AllFiles/Clients/ISF/" + guid);
                fuLink_To_ISF.SaveAs(guidPath);
                isFileSaved = true;
            }
        }

        if (!isFileSaved)
            guid = "";

        return guid;
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_client_details model = new tbl_client_details();
        model.USER_CODE = id.ToString();

        BLL_tbl_client_details bll = new BLL_tbl_client_details();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }

    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_client_details bll = new BLL_tbl_client_details();
                List<tbl_client_details> list = bll.select(id);
                foreach (tbl_client_details tbl in list)
                {
                    btnSave.CommandArgument = tbl.USER_CODE.ToString();
                    txtUSER_CODE.Text = tbl.USER_CODE;
                    txtUSER_CODE.Enabled = false;
                    txtCOMPANY_NAME.Text = tbl.COMPANY_NAME;
                    txtCOMPANY_ADDRESS.Text = tbl.COMPANY_ADDRESS;
                    txtPOSTAL_CODE.Text = tbl.POSTAL_CODE;
                    txtPRIMARY_TELEPHONE.Text = tbl.PRIMARY_TELEPHONE;
                    txtSECONDARY_TELEPHONE.Text = tbl.SECONDARY_TELEPHONE;
                    txtPRIMARY_MOBILE.Text = tbl.PRIMARY_MOBILE;
                    txtSECONDARY_MOBILE.Text = tbl.SECONDARY_MOBILE;
                    chkActive.Checked = tbl.Active;
                    txtEmail.Text = tbl.Email;


                    fuAgreement_Document.Style.Add("display", "none");
                    txtAgreement_Document.Style.Remove("display");
                    btnRemoveAgreement.Style.Remove("display");
                    txtAgreement_Document.Attributes["OriginalFileName"] = tbl.Agreement_Document;
                    txtAgreement_Document.Attributes["GuidFileName"] = tbl.Agreement_Document_GUID;

                    fuLink_To_ISF.Style.Add("display", "none");
                    txtLink_To_ISF.Style.Remove("display");
                    btnRemoveISF.Style.Remove("display");
                    txtLink_To_ISF.Attributes["OriginalFileName"] = tbl.Link_To_ISF;
                    txtLink_To_ISF.Attributes["GuidFileName"] = tbl.Link_To_ISF_GUID;

                    string agreementURL = ResolveUrl("~/AllFiles/Clients/Agreement/" + tbl.Agreement_Document_GUID);
                    string isfURL = ResolveUrl("~/AllFiles/Clients/ISF/" + tbl.Link_To_ISF_GUID);

                    txtAgreement_Document.Text = "<a href='"+ agreementURL +"'>" + tbl.Agreement_Document + "</a>";
                    txtLink_To_ISF.Text = "<a href='"+ isfURL +"'>" + tbl.Link_To_ISF + "</a>";

                    txtFAX.Text = tbl.FAX;
                    txtLinkedIn_ID.Text = tbl.LinkedIn_ID;
                    txtSkype_ID.Text = tbl.Skype_ID;
                    txtVAT_Tax_ID.Text = tbl.VAT_Tax_ID;
                    ddlCountry.SelectedValue = tbl.Country;
                    ViewState["Country"] = tbl.Country;
                    ddlCountry_SelectedIndexChanged(null, null);
                    ddlSTATE.SelectedValue = tbl.STATE;
                    ViewState["STATE"] = tbl.STATE;
                    ddlSTATE_SelectedIndexChanged(null, null);
                    ddlCITY.SelectedValue = tbl.CITY;
                    //if (tbl.Link_To_ISF.Trim().Length > 0)
                    //{
                    //    fuLink_To_ISF.Visible = false;
                    //    txtLink_To_ISF.Visible = true;
                    //}
                    //if (tbl.Agreement_Document.Trim().Length > 0)
                    //{
                    //    fuAgreement_Document.Visible = false;
                    //    txtAgreement_Document.Visible = true;
                    //}
                }
            }
        }
    }

    private void bindState(string Country)
    {
        ddlSTATE.Items.Clear();
        ddlSTATE.DataSourceID = null;
        ddlSTATE.DataValueField = "State";
        ddlSTATE.DataTextField = "State";
        ddlSTATE.DataSource = DBUtil.ExecuteDataTable("Select distinct State from tbl_countrystatemapping where Country='" + Country + "' order by State");
        ddlSTATE.DataBind();
        ddlSTATE.Items.Insert(0, new ListItem("--Select--", "0"));
    }

    private void bindCity(string Country, string State)
    {
        ddlCITY.Items.Clear();
        ddlCITY.DataSourceID = null;
        ddlCITY.DataValueField = "City";
        ddlCITY.DataTextField = "City";
        ddlCITY.DataSource = DBUtil.ExecuteDataTable("Select distinct City from tbl_countrystatemapping where Country='"+ Country +"' and State='"+ State +"' order by City");
        ddlCITY.DataBind();
        ddlCITY.Items.Insert(0, new ListItem("--Select--", "0"));
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Request.QueryString["editRecord"] != null)
        {
            if (ViewState["Country"] != null)
                bindState(ViewState["Country"].ToString());
            else
                bindState(ddlCountry.SelectedValue);
        }
    }

    protected void ddlSTATE_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Request.QueryString["editRecord"] != null)
        {
            if (ViewState["STATE"] != null && ViewState["Country"] != null)
                bindCity(ViewState["Country"].ToString(), ViewState["STATE"].ToString());
            else
                bindCity(ddlCountry.SelectedValue, ddlCountry.SelectedValue);
        }
    }
}